﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace Player
{
    public class LocationConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double value = values[0] == DependencyProperty.UnsetValue ? 0 : (double)values[0];
            if (value > 1) value = 1;
            if (value < 0 || double.IsNaN(value)) value = 0;
            double width = (double)values[1];

            System.Diagnostics.Debug.Assert(value <= 1);
            return width * value;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class LengthConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double start = values[0] == DependencyProperty.UnsetValue ? 0 : (double)values[0];
            double end = values[1] == DependencyProperty.UnsetValue ? 0 : (double)values[1];
            if (start > 1) start = 1;
            if (start < 0) start = 0;
            if (end > 1) end = 1;
            if (end < start) end = start;
            double length = end - start;
            double width = (double)values[2];

            return width * length;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class TimeConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime startTime = values[0] == DependencyProperty.UnsetValue ? DateTime.MinValue : (DateTime)values[0];
            DateTime endTime = values[1] == DependencyProperty.UnsetValue ? DateTime.MinValue : (DateTime)values[1];
            double value = values[2] == DependencyProperty.UnsetValue ? 0 : (double)values[2];
            if (value > 1) value = 1;
            if (value < 0) value = 0;
            TimeSpan duration = endTime - startTime;
            DateTime pointedTime = startTime + TimeSpan.FromTicks((long)((double)duration.Ticks * value));

            return pointedTime.ToString();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BubbleRectangle : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double width = (double)values[0];
            double height = (double)values[1];
            return new Rect(new Point(0, 0), new Point(width + 20, height + 20));
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class BubbleLocation : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double height = (double)value;
            return -height - 5; // Leave 5 point space
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class ObjectToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return Visibility.Collapsed;
            else return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
